package com.bootcamp.mall.controller.admin;

/**
 * @program: mall
 * @author: Keyu Li
 * @create: 2021-11-09 17:24
 **/

import com.bootcamp.mall.model.Result;
import com.bootcamp.mall.model.vo.user.AllUserVO;
import com.bootcamp.mall.service.user.UserService;
import com.bootcamp.mall.service.user.UserServiceImpl;
import com.bootcamp.mall.utils.StringUtil;
import com.google.gson.Gson;

import javax.servlet.*;
import javax.servlet.http.*;
import javax.servlet.annotation.*;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.List;

@WebServlet(name = "UserServlet", value = "/api/admin/user/*")
public class UserServlet extends HttpServlet {
    // UserService 具体实现类
    UserService userService = new UserServiceImpl();
    // gson
    Gson gson = new Gson();

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取具体servlet
        String op = request.getRequestURI().replace("/api/admin/user/", "");
        if ("allUser".equals(op)) {
            opAllUsers(request, response);
        } else if ("searchUser".equals(op)) {
            opSearchUser(request, response);
        } else if ("deleteUser".equals(op)) {
            opDeleteUser(request, response);
        }
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // 获取具体servlet
    }



    private void opDeleteUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        // 根据id删除用户
        int id;
        try {
            id = Integer.parseInt(request.getParameter("id"));
        }catch (NumberFormatException e){
            writer.println(gson.toJson(Result.error("前端参数异常！")));
            return;
        }
        Integer code = userService.deleteUser(id);
        if (code == 1) {
            // 删除成功
            writer.println(gson.toJson(new Result(0, null, null)));
            return;
        } else if (code == -1) {
            // 删除admin用户
            writer.println(gson.toJson(new Result(400, "禁止删除admin用户！", null)));
            return;
        }
        writer.println(gson.toJson(new Result(500, "未知异常！", null)));
    }

    private void opSearchUser(HttpServletRequest request, HttpServletResponse response) throws IOException {
        PrintWriter writer = response.getWriter();
        // 根据nickname删除用户
        String nickname = request.getParameter("word");
        List<AllUserVO> allUserVOs = userService.searchUser(nickname);
        // 不管返回null，还是返回vo集合，都可以正常返回
        response.getWriter().println(gson.toJson(new Result(0, null, allUserVOs)));
    }

    private void opAllUsers(HttpServletRequest request, HttpServletResponse response) throws IOException {
        List<AllUserVO> allUserVOs = userService.allUser();
        // 不管返回null，还是返回vo集合，都可以正常返回
        Result result = new Result();
        result.setCode(0);
        result.setData(allUserVOs);
        response.getWriter().println(gson.toJson(result));
    }


}
